package com.example.lastwork2.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.telephony.SmsManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.example.lastwork2.R;
import com.example.lastwork2.fragments.recfragments.RecRecFragment;
import com.example.lastwork2.model.Shop;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

public class RecAdapter extends BaseAdapter {


    //获取上下文环境
    private Context context;
    //item布局资源
    private int layoutId;
    //数据源
    private List<Shop> shops;
//
//    //用于子线程想主线程发送数据
//    private Handler handler;



    public RecAdapter(){}

    public RecAdapter(Context context, int layoutId, List<Shop> shops) {
        this.context = context;
        this.layoutId = layoutId;
        this.shops = shops;
    }

    @Override
    public int getCount() {
        return shops.size();
    }

    @Override
    public Object getItem(int position) {
        return shops.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //1.获取布局文件
        View view = LayoutInflater.from(context).inflate(layoutId,null);
        //2.绑定控件
        ImageView shopIcon = view.findViewById(R.id.shopIcon);
        TextView shopName = view.findViewById(R.id.shopName);
        TextView shopBrief = view.findViewById(R.id.shopBrief);
        TextView shopPrice = view.findViewById(R.id.shopPrice);
        //3.获取数据源中当前位置的元素
        Shop shop = shops.get(position);

        //4.把数据放入对应控件中
        Glide.with(context)
                .load(shop.getIcon())
                .into(shopIcon);

        shopName.setText(shop.getShopName());
        shopBrief.setText(shop.getShopBrief());
        String price = String.valueOf(shop.getPrice());
        shopPrice.setText(price);


        return view;
    }



}
